package com.tanhua.server.service;

import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.model.db.*;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.model.vo.UserInfoVoAge;
import com.tanhua.server.interceptor.UserHolder;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.data.config.ConfigurationUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;


import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SettingsService {



    @DubboReference
    private SettingsApi settingsApi;

    @DubboReference
    private QuestionApi questionApi;

    @DubboReference
    private BlackListApi blackListApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    public SettingsVo findSettings() {
        /**
         * 用户通用设置 - 读取
         */
            SettingsVo vo = new SettingsVo();
            //1.获取用户数据
            User user = UserHolder.getUser();
            Long userId = user.getId();
            //2.获取陌生人问题（根据当前用户id查询tb_question表得到问题内容）
            Question question = questionApi.findQuestionById(userId);
            //2.1 为了需求给设置默认值
            String txt = ("今日几号");
            if (question != null) {
                txt = question.getTxt();
            }
            //3.获取通知设置数据（根据当前用户id查询tb_settings表得到设置内容）
            Settings settings = settingsApi.findSettingsById(userId);
            if (settings != null){
                BeanUtils.copyProperties(settings,vo);
            }
            vo.setId(userId);
            vo.setStrangerQuestion(txt);
            vo.setPhone(user.getMobile());
            return vo;

    }

    /**
     * 黑名单 - 翻页列表
     */
    public PageResult<UserInfoVoAge> findPageBlackList(Long page, Long pagesize) {

        //1.分页查询黑名单分页列表数据

        PageResult<BlackList>  pageBlackList =  blackListApi.findPageBlackList(page,pagesize,UserHolder.getUserId());
        //防止为空
        if (pageBlackList==null || CollectionUtils.isEmpty(pageBlackList.getItems())){
            return null;
        }

        //2.从第一步中获取所有黑名单用户ids
        // List<BlackList> blackList = pageBlackList.getItems();
        // ArrayList<Long> listBlackListUserId = new ArrayList<>();
        // for (BlackList list : blackList) {
        //     Long blackUserId = list.getBlackUserId();
        //     listBlackListUserId.add(blackUserId);
        // }

        //方式二：stream方式  BlackList::getBlackUserId(获取什么数据) Collectors.toList()变为list集合
        List<Long> listBlackUserId = pageBlackList.getItems().stream().map(BlackList::getBlackUserId).collect(Collectors.toList());

        //3.根据第二步的所有黑名单用户ids查询用户详情
        //select * from tb_userinfo where id in(1,2,3,4,.......)
        List<UserInfo> userInfoList = userInfoApi.findByBatchIds(listBlackUserId);

        //3.1 将List<UserInfo> 转为 List<UserInfoVoAge>
        List<UserInfoVoAge> infoVoAgeList = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            UserInfoVoAge voAge = new UserInfoVoAge();
            BeanUtils.copyProperties(userInfo,voAge);//头像 昵称 性别 年龄
            voAge.setId(userInfo.getId());//黑名单用户id
            log.debug("以下是黑名单的id");
            System.out.println(userInfo.getId());
            infoVoAgeList.add(voAge);
        }
        //4.将PageResult<BlackList> 转为 PageResult<UserInfoVoAge>
        return new PageResult<>(pageBlackList.getPage(),pageBlackList.getPagesize(),pageBlackList.getCounts(),infoVoAgeList);
    }


    }
